import request from '@/utils/request'

// 查询博客文章列表
export function listArticle(query) {
  // 创建新的查询参数对象
  const params = { ...query }
  
  // 如果 userId 字段实际上是作者姓名，重命名为合适的参数名
  if (params.userId) {
    params.authorName = params.userId
    delete params.userId
  }
  console.log(params);
  
  return request({
    url: '/systemArticle/article/list',
    method: 'get',
    params: params
  })
}

// 查询博客文章详细
export function getArticle(id) {
  return request({
    url: '/systemArticle/article/' + id,
    method: 'get'
  })
}

// 新增博客文章
export function addArticle(data) {
  return request({
    url: '/systemArticle/article',
    method: 'post',
    data: data
  })
}

// 修改博客文章
export function updateArticle(data) {
  return request({
    url: '/systemArticle/article',
    method: 'put',
    data: data
  })
}

// 删除博客文章
export function delArticle(id) {
  return request({
    url: '/systemArticle/article/' + id,
    method: 'delete'
  })
}

export function getUserNameById(userId) {
  return request({
    url: '/systemArticle/article/getUserNameById/' + userId,
    method: 'get'
  })
}

// 根据分类ID获取分类名称
export function getCategoryNameById(categoryId) {
  return request({
    url: '/systemArticle/article/getCategoryNameById/' + categoryId,
    method: 'get'
  })
}

export function getCategoryList() {
  return request({
    url: '/systemArticle/article/getCategoryList/',
    method: 'get'
  })
}

export function upload(file) {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: '/systemArticle/article/upload/',
    method: 'post',
    data: formData
    // 注意：不要手动设置 Content-Type，让浏览器自动设置 multipart/form-data
  })
}